<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/20
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;
use think\facade\Cache;

class RegionsModel extends Model
{

    protected $autoWriteTimestamp = true;
    protected $deleteTime = 'delete_time';

    public static function getRegionDate(){

        //Cache::rm('public_city');

        if (!Cache::get('public_city')) {

            $list=self::getChild();

            Cache::set('public_city', $list);
        }
        return Cache::get('public_city');
    }

    private static function getChild($type=1,$pid=''){
        $arr=[];
        $qure=self::where('region_type',$type);

        if($type>1){
            $qure->where('parent_id',$pid);
        }

        $list=$qure->select();

        if(empty($list)){
            return false;
        }

        foreach ($list as $vo){
            $data=[
                'name'=>$vo['region_name'],
                'key'=>$vo['region_id']
            ];

            if(($type+1)<4){
                $child=self::getChild($type+1,$vo['region_id']);
                if($child){
                    $data['child']=$child;
                }
            }

            $arr[]=$data;
        }

        return $arr;
    }

    public static function getAll($pid=''){

        $qure=self::where('region_id','<>',0);

        if(empty($pid)){
            $qure->where('region_type',1);
        }else{
            $qure->where('parent_id',$pid);
        }
        $list=$qure->select();

        return $list;
    }

}